﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Script.Serialization;
using Medora.Model.Basic;
using Medora.Model.Institutions;
using Medora.Services;

namespace MvcApp.Controllers
{
    [RoutePrefix("api/Departments")]
    public class DepartmentsController : BaseApiController
    {
        private readonly IDepartmentsService _service;
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        // GET api/<controller>
        public DepartmentsController(IDepartmentsService service)
        {
            _service = service;
        }

        [Route("GetByPage")]
        public IHttpActionResult GetByPage(int pageSize, int page, string filter, string ss)
        {
            var userId = GetCurrentUserId();
            if (userId != null && userId != Guid.Empty)
            {
                var departmentFilter = serializer.Deserialize<DepartmentFilter>(filter);
                SortInfo sort = null;
                if (!string.IsNullOrEmpty(ss)) sort = serializer.Deserialize<SortInfo>(ss);
                var result= _service.GetPagedByInstitutionByUserId(pageSize, page, userId.Value, departmentFilter, sort);
                return Ok(result);
            }
            return BadRequest("Authorization Error");
        }

        [Route("GetCount")]
        public IHttpActionResult GetCount()
        {
            var userId = GetCurrentUserId();
            if (userId != null && userId != Guid.Empty)
            {
                return Ok(_service.GetCount(userId.Value));
            }
            return BadRequest("Authorization Error");
        }

        // POST api/<controller>
        public IHttpActionResult Post([FromBody]Department value)
        {
            if (ModelState.IsValid)
            {
                var userId = GetCurrentUserId();
                if (userId != null && userId != Guid.Empty)
                {
                    if (_service.Save(userId.Value, value)) return Ok(value);
                }
                return BadRequest("Authorization Error");
            }
            return BadRequest("Validation error");
        }

        // DELETE api/<controller>/5
        [HttpDelete]
        public void Delete(string id)
        {
            var ids = serializer.Deserialize<List<Guid>>(id);
            if (_service.Delete(ids)) Request.CreateResponse(HttpStatusCode.OK);

        }

        [Route("GetAutocomplete")]
        public IHttpActionResult GetAutocomplete(string query)
        {
            var autoComplete = new AutoComplete<AutoCompleteItem> { query = query, suggestions = new List<AutoCompleteItem>() };
            var userId = GetCurrentUserId();
            if (userId != null && userId != Guid.Empty)
            {
                var lst = _service.GetByInstitutionByUserId(userId.Value).Where(o => o.Title.ToLower().Contains(query.ToLower())).Select(o => new AutoCompleteItem { data = o.Id.ToString(), value = o.Title });
                autoComplete.suggestions.AddRange(lst);
            }
            return Ok(autoComplete);
        }

        [Route("GetLazy")]
        public IHttpActionResult GetLazy()
        {
            var userId = GetCurrentUserId();
            if (userId != null && userId!=Guid.Empty)
            {
                var result= _service.GetByInstitutionByUserId(userId.Value).Select(o => new AutoCompleteItem { data = o.Id.ToString(), value = o.Title }).OrderBy(o => o.value);
                return Ok(result);

            }
            return BadRequest("Authorization Error");
        }

        [Route("GetByInstitution")]
        public IHttpActionResult GetByInstitution(Guid id)
        {
            var result = _service.GetByInstitution(id).Select(o => new AutoCompleteItem { data = o.Id.ToString(), value = o.Title }).OrderBy(o => o.value);
            return Ok(result);
        }
        
    }
}